﻿using System.Collections.Generic;
using System.Threading.Tasks;

using EFCore.BulkExtensions;

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Guids;
using Volo.Abp.Timing;
using Volo.Abp.Users;

namespace Hang.Blog.DomainPlus.EntityPlus {
    public static class RepositoryPlus {

        private static IClock _clock;
        private static ICurrentUser _currentUser;
        private static IGuidGenerator _guidGenerator;

        public static void UseRepositoryPlus(this IApplicationBuilder app) {
            _clock = app.ApplicationServices.GetRequiredService<IClock>();
            _currentUser=app.ApplicationServices.GetRequiredService<ICurrentUser>();
            _guidGenerator=app.ApplicationServices.GetRequiredService<IGuidGenerator>();

        }

        public static async Task BulkInsert<T>(this IRepository<T> repository, IList<T> entites) where T : class, IEntity {
            var ctx = await repository.GetDbContextAsync();
            await ctx.BulkInsertAsync(entites);
            await ctx.SaveChangesAsync();
        }

        public static async Task BulkUpdateAsync<T>(this IRepository<T> repository, IList<T> entites) where T : class, IEntity {
            var ctx = await repository.GetDbContextAsync();
            await ctx.BulkUpdateAsync(entites);
            await ctx.SaveChangesAsync();
        }


        public static async Task DeleteSoft<T>(this IRepository<T> repository, IList<T> entites) where T : class, IEntity {
            var ctx = await repository.GetDbContextAsync();
            await ctx.BulkInsertAsync(entites);
            await ctx.SaveChangesAsync();
        }

        public static async Task BulkHardDelete<T>(this IRepository<T> repository, IList<T> entites) where T : class, IEntity {
            var ctx = await repository.GetDbContextAsync();
            await ctx.BulkUpdateAsync(entites);
            await ctx.SaveChangesAsync();
        }
    }


    public interface IPage {
         public int Page { get; set; }  

         public int PageSize { get;set; }
    }
}
